from DrissionPage import ChromiumPage
from DrissionPage.common import ActionChains
from DrissionPage.common import Keys
from TimePinner import Pinner
import logging as log
log.basicConfig(level=log.INFO,format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

page = ChromiumPage()
ac = ActionChains(page)
pinner = Pinner()

# 综合热门url
url = "https://www.bilibili.com/v/popular/all"

def check_more():
    return page.ele('x://div[@class="no-more"]',timeout=1)


def main():
    # 获取当前url
    current_url = page.url.split("/?")[0]
    log.info(current_url)
    if url != current_url:
        page.get(url)
    while not check_more():
        log.info("scroll...")
        ac.type(Keys.PAGE_DOWN)
        #ac.scroll(delta_y=
    pinner.pin()

    vedios = page.s_eles('x:////ul[@class="card-list"]/div')
    for item in vedios:
        # 视频名称
        vedio_name = item.ele('x://p[@class="video-name"]/text()')
        # 视频url
        vedio_url = item.ele('x://div[@class="video-card__content"]/a/@href')
        # bv号
        bv = vedio_url.split("/")[-1]
        # 封面url
        data_src = item.ele('x://div[@class="video-card__content"]/img/@data-src')
        # up主名称
        up_name = item.ele('x://span[@class="up-name__text"]/text()')
        # 播放数
        play_num = item.ele('x://p[@class="video-stat"]/span[1]/text()').strip()
        # 弹幕数
        danmu_num = item.ele('x://p[@class="video-stat"]/span[2]/text()').strip()
        log.info(vedio_name)
        log.info(vedio_url)
        log.info(bv)
        log.info(data_src)
        log.info(up_name)
        log.info(play_num)
        log.info(danmu_num)
    pinner.pin('用时')




if __name__=="__main__":
    main()